package com.rqbl.service.impl;

import com.rqbl.entity.Comment;
import com.rqbl.mapper.CommentMapper;
import com.rqbl.myutils.PageBean;
import com.rqbl.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bruce
 * @since 2024-07-19
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 获得评论数据A
     * @param pageBean
     * @param pid
     * @return
     */
    @Override
    public List<Comment> getCommentByPage(PageBean pageBean, Integer pid) {
        pageBean.setTotalSize(commentMapper.getCommentCount());
        System.out.println(pageBean.getTotalSize());
        int totalPage = pageBean.getTotalSize() / pageBean.getPageSize();
        if(pageBean.getTotalSize() % pageBean.getPageSize() != 0) {
            totalPage++;
        }
        pageBean.setTotalPage(totalPage);
        System.out.println("当前位序"+(pageBean.getPage()-1)*pageBean.getPageSize());
        System.out.println("当前页"+pageBean.getPage());
        System.out.println("pid"+pid);
        System.out.println(commentMapper.getCommentByPage((pageBean.getPage()-1)*pageBean.getPageSize(), pageBean.getPageSize(),pid).toString());
        return commentMapper.getCommentByPage((pageBean.getPage()-1)*pageBean.getPageSize(), pageBean.getPageSize(),pid);
    }


    /**
     * 添加评论
     *
     * @param text
     * @param url
     * @param uid
     * @param pid
     */
    @Override
    public Integer addComment(String text, String url, Integer uid, Integer pid) {
        Comment comment = new Comment();
        comment.setCimageurl(url);
        comment.setCtext(text);
        comment.setPid(pid);
        comment.setUid(uid);
        commentMapper.addComment(comment);
        return comment.getCid();
    }
}
